<?php
namespace App\Task;

use Hyperf\Crontab\Annotation\Crontab;

use App\Query\OrderQuery;
use App\Query\UserQuery;
use App\Repository\UserRepository;

/**
 * @Crontab(name="UserLockTask", rule="0 1 * * *", callback="execute", memo="用户静态时间冻结检测")
 */
class UserLockTask
{
    public function execute()
    {
        $repository = UserRepository::make();
        
        // 寻找未锁定的用户并且有等级的
        $users = UserQuery::moneyRemainTime(UserQuery::lockNo(
            UserQuery::level(UserQuery::model(), 2)
        ))->get();
        
        // 循环检查的用户
        foreach($users as $user)
        {
            // 获取用户是否还有可释放的订单
            if(!OrderQuery::remain(OrderQuery::user($user))->count())
            {
                // 如果没有，那就冻结帐号
                $repository->model($user);
                
                // 冻结
                $repository->setLockStatus();
                
                var_dump($user->username . ' 帐号被冻结');
            }
        }
    }
}